<?php
defined('BY_JYA') or exit('error');
$merchid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单','error','/manage.order_list/');
	}
	$pay_types=get_pay_types();
	$ispay=array('0'=>'未支付','1'=>'已支付');
	$item=model('jy_weishop/order')->get_order($id,$merchid);
	foreach($item['goods'] as &$gv){
		$gv['title'].='['.$gv['optiondata'].']';
	}
	$item['status_title']=$this->order_status[$item['status']];
	if($isonlyverifygoods){
		$item['step3title']='核销时间';
	}else if($item['isverify']==1){
		$item['step3title']='确认使用';
	}
	else if($item['addressid']){
		$item['step3title']='商家发货';
	}
	else if($item['isvirtualsend'] && $item['virtual']){
		$item['step3title']='自动发货';
	}else{
		$item['step3title']='确认取货';
	}
	if($item['isverify']==1){
		$item['sendtypetitle']='线下核销';
	}else if($item['addressid']){
		$item['sendtypetitle']='快递';
	}else if($item['isvirtualsend'] && $item['virtual']){
		$item['sendtypetitle']='自动发货(虚拟物品)';
	}else if($item['isverify']==1){
		$item['sendtypetitle']='线下核销';
	}else if($item['dispatchtype']){
		$item['sendtypetitle']='自提';
	}else{
		$item['sendtypetitle']='其他';
	}
	if($item['status']==0 && $item['paytype']==3){
		$item['statustitle']='待发货';
	}elseif($item['status']==0 && $item['paytype']!=3){
		$item['statustitle']='待付款';
	}elseif($item['status']==1 && $item['isverify']==1){
		$item['statustitle']='待使用';
	}elseif($item['status']==1 && $item['sendtype']==2){
		$item['statustitle']='待取货';
	}elseif($item['status']==1 && $item['sendtype']!=2 && $item['isverify']!=1 && $item['addressid']){
		if($item['sendtype']==3){
			$item['statustitle']='虚拟发货';
		}elseif($item['send_status']==0){
			$item['statustitle']='待发货';
		}else{
			$item['statustitle']=$this->send_status[$item['send_status']];
		}	
	}elseif($item['status']==2){
		$item['statustitle']='待收货';
	}elseif($item['status']==3){
		$item['statustitle']='交易完成';
	}elseif($item['status']==-1 || $item['status']==4){
		$item['statustitle']='已关闭';
	}else{
		$item['statustitle']=$this->order_status[$item['status']];
	}
	$item['paytype']=$pay_types[$item['paytype']];
	$item['member']=model('core/member')->detail($item['mid'],array('nickname','avatar','mobile'));
	//$item['address']=model('core/member')->get_address_str($item['addressid']);
	if($item['address']){
		$address=json_decode($item['address'],true);
	}else{
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['addressid']));
		$address['pcd']=json_decode($address['pcd'],true);		
	}
	$addressdesc=model('core/member')->get_address_str($address);
	$item['address']=array(
		'address'=>$addressdesc,
		'mobile'=>$address['mobile'],
		'name'=>$address['name'],
	);
	if($_SESSION['os']=='mobile'){
			$item['createtime']=date('Y-m-d H:m:s',$item['createtime']);
			$item['paytime']=date('Y-m-d H:m:s',$item['paytime']);
			$item['finishtime']=date('Y-m-d H:m:s',$item['finishtime']);
	}
	$htmls=array();
	if($item['status']==1){
		//待发货
		$htmls[]=array(
			'type'=>'button_list',
			'list'=>array(
				array('type'=>'modal','url'=>'/order.list/fa/id='.$item['id'],'title'=>'发货'),
			),
		);
	}
	$htmls[]=array(
		'type'=>'look',
		'desc'=>'基本信息',		
		'look'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member.nickname','title'=>'买家昵称','type'=>'string'),
			array('name'=>'member.avatar','title'=>'买家头像','type'=>'image'),
			array('name'=>'member.mobile','title'=>'买家电话','type'=>'string'),
			array('name'=>'price','title'=>'订单金额','type'=>'string'),
			array('name'=>'goodsprice','title'=>'商品金额','type'=>'string'),
			array('name'=>'discountprice','title'=>'运费','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'status_title','title'=>'订单状态','type'=>'string'),
		)
	);
	//商品信息
	$htmls[]=array(
		'type'=>'look',
		'desc'=>'商品清单',		
		'look'=>array(
			array('name'=>'goods','title'=>'商品信息','type'=>'array','list'=>array(
				array('type'=>'string','name'=>'title','title'=>'商品名称'),
				array('type'=>'image','name'=>'image','title'=>'商品图片'),
				array('type'=>'string','name'=>'price','title'=>'购买单价'),
				array('type'=>'string','name'=>'total','title'=>'购买数量'),
			)),
		)
	);
	//支付
	$pay_show=array(
		'type'=>'look',
		'desc'=>'支付',
		'look'=>array(
			array('name'=>'ispay','title'=>'是否支付','type'=>'string'),
		),
	);
	if($item['ispay']){
		$pay_show['look'][]=array('name'=>'paytype','title'=>'支付方式','type'=>'string');
		$pay_show['look'][]=array('name'=>'paytime','title'=>'支付时间','type'=>'time');
	}
	$item['ispaytitle']=$ispay[$item['ispay']];
	$htmls[]=$pay_show;
	//收货信息
	$htmls[]=array(
		'type'=>'look',
		'desc'=>'收货信息',
		'look'=>array(
			array('name'=>'address.address','title'=>'收货地址','type'=>'string'),
			array('name'=>'address.name','title'=>'收货人姓名','type'=>'string'),
			array('name'=>'address.mobile','title'=>'收货人电话','type'=>'string'),
		),
	);
	$express=pdo_get('jy_weishop_order_express',array('orderid'=>$item['id']));
	if($express){
		//发货信息
		$express_types=model('jy_weishop/model')->express_types();
		$express['express_id']=$express_types[$express['express_id']]['name'];
		$item['express']=$express;
		$htmls[]=array(
			'type'=>'look',
			'desc'=>'发货货信息',
			'look'=>array(
				array('name'=>'express.createtime','title'=>'发货时间','type'=>'time'),
				array('name'=>'express.express_id','title'=>'快递公司','type'=>'string'),
				array('name'=>'express.expresssn','title'=>'快递单号','type'=>'string'),
			),
		);
	}
	//$return['htmls']=$htmls;
	$item['diyformdata']=json_decode($item['diyformdata'],true);
	if($item['sendtype']==1){
		if($item['peisong_id']){
			if(!$this->plugin_exists('jy_peisong')){
				exi('请联系管理员开通《配送员》插件！','error');
			}
			$return['peisong']=pdo_get('jy_peisong_member',array('mid'=>$item['peisong_id']));
		}else{
			$return['peisong']=pdo_get('jy_weishop_order_express',array('orderid'=>$item['id']));
		}
	}
	//当前桌号：
	if($item['tableid']){
		$table=pdo_get('jy_weishop_table',array('id'=>$item['tableid'],'uniacid'=>$_SESSION['uniacid']));
		$group=pdo_get('jy_weishop_table_group',array('id'=>$table['group_id'],'uniacid'=>$_SESSION['uniacid']));
		$return['tablename']=$group['title'].'('.$table['title'].')';
	}
	$order_gifts=pdo_get('jy_weishop_order_gift',array('order_id'=>$item['id']));
	if($order_gifts){
		$order_gifts=json_decode($order_gifts['gifts'],true);
		if($_SESSION['is_tomedia']){
		foreach($order_gifts as &$giv){
			$giv['image']=$giv['image']?tomedia($giv['image']):'';
		}
		}
	}
	$return['order_gifts']=$order_gifts;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='receive'){
	$post=get_postdata();
	$id=intval($post['id']);
	model('jy_weishop/order')->order_receive($id);
	exi('收货成功！');	
}
if($_RQ['op']=='cancelfh'){
	$post=get_postdata();
	$id=intval($post['id']);
	model('jy_weishop/order')->order_cancelfh($id);
	exi('取消发货成功！','','/manage.order_list/');	
}
if($_RQ['op']=='upaddress'){
	$addressid=intval($_RQ['addressid']);
	$post=get_postdata();
	if($post['price']){
		pdo_update('jy_weishop_order',array('price'=>$post['price']),array('id'=>$post['id']));
		exi('修改收货信息成功！','','/manage.order_list/');	
	}
	$item=pdo_get('core_address',array('id'=>$addressid));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改收货信息',
	);
	$htmls['edit']=array(
		array('type'=>'string','title'=>'价格','name'=>'price','help'=>'修改价格！'),	
	);
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/order_detail');
	}else{
		$return['htmls']=$htmls;
	}
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='remark'){
	$id=intval($_RQ['id']);
	$post=get_postdata();
	if($post['remark']){
		pdo_update('jy_weishop_order',array('remark'=>$post['remark']),array('id'=>$post['id']));
		exi('备注成功！','','/manage.order_list/');	
	}
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','remark'));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改备注',
	);
	$htmls['edit']=array(
		array('type'=>'text','title'=>'备注内容','name'=>'remark','help'=>'填写备注内容！'),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='upprice'){
	$id=intval($_RQ['id']);
	$post=get_postdata();
	if($post['price']){
		if(!is_numeric($post['price'])){
			exi('价格必须是数字！','error');
		}
		if(!method_exists(cfc('pay'),'change_pay_fee')){
			exi('修改价格失败！请联系管理员，更新框架到最新','error');
		}
		$price=floatval($post['price']);
		if ($price < 0) 
		{
			$price = 0;
		}
		model('jy_weishop/order')->upprice($post['id'],$price);
		exi('改价成功！','','/manage.order_detail//id='.$post['id']);	
	}
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','price'));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改价格',
	);
	$htmls['edit']=array(
		array('type'=>'string','title'=>'价格','name'=>'price','help'=>'修改价格！'),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='lookexpress'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order_express',array('orderid'=>$id));
	$express_types=model('jy_weishop/model')->express_types();
	$item['express_name']=$express_types[$item['express_id']]['name'];
	$item['express']=$express_types[$item['express_id']]['express'];
	$return['expresslist']=model('jy_weishop/model')->getExpressList($item['express'],$item['expresssn']);
	$return['item']=$item;
	exi($return);
}